function [D, wd] = make_withinclass_diffvecs(X, ginds, cmeans, w)
%MAKE_WITHINCLASS_DIFFVECS Creates a matrix containing the within-class
%differences
%
% [ Syntax ]
%   - [D, w] = make_withinclass_diffvecs(X, ginds, cmeans, w)
%
% [ History ]
%   - Created by Dahua Lin, on Jul 23, 2007
%

g = cellfun(@(x) x(:), ginds, 'UniformOutput', false);
px = vertcat(g{:});
pc = slexpand((1:numel(ginds))', cellfun(@numel, ginds), 1);
D = X(:, px) - cmeans(:, pc);

if isempty(w)
    wd = [];
else
    wd = w(px);
end

    